package com.orm.analysis.service;

import com.orm.analysis.dao.OrderDAO;
import com.orm.analysis.dao.PaymentDAO;
import com.orm.analysis.model.*;
import com.orm.analysis.util.GlobalConst;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: saman
 * Date: 10/12/13
 * Time: 7:29 AM
 * To change this template use File | Settings | File Templates.
 */
public class PaymentService   implements GenericService<Payment> {


    private static Logger log = Logger.getLogger(PaymentService.class);

    private String paymentDataFielDir;

    private PaymentDAO dao;

    public PaymentDAO getDao() {
        return dao;
    }

    public void setDao(PaymentDAO dao) {
        this.dao = dao;
    }

    public String getPaymentDataFielDir() {
        return paymentDataFielDir;
    }

    public void setPaymentDataFielDir(String paymentDataFielDir) {
        this.paymentDataFielDir = paymentDataFielDir;
    }

    public Payment create(Payment entity) {
        dao.save(entity);
                return entity;
            }


    public void delete(Object id) {

        dao.delete(id);
    }
    public Payment update(Payment entity) {
        dao.update(entity);
            return entity;
        }

    public List<Payment> list(int page, int size) {
        return dao.list(page,size);
    }

    public Payment getById(Payment entity) {
        return dao.getById(entity.getPaymentId());
    }

    public Payment getById(Integer id) {
        return dao.getById(id);
            }


    public void executeImproter() throws ParseException, IOException {
        try{
                log.info("File  >>" + paymentDataFielDir);
                BufferedReader br = null;

                br = new BufferedReader(new FileReader(paymentDataFielDir));

                StringBuilder sb = new StringBuilder();
                String line = br.readLine();
                line = br.readLine();//Skip 1 line

                while (line != null) {
                    log.info("File  >>" + line);
                    //ID( not need) AMOUNT,PAY_DATE,CHECK_NUMBER,BANK_NAME,BRANCH_NAME,CLEARD,CRECIT_CARD_NUMBER,EXPIRE_DATE,CREDIT_CARD_TYPE

                    String[] data = line.split("[,]");

                    if(data[0].equalsIgnoreCase("CASH")) {
                        Payment payment = new Payment();
                        payment.setAmount(Double.valueOf(data[1]));
                        payment.setPaymentDate(GlobalConst.simpleDateFormat.parse(data[2].trim()));
                        dao.save(payment);
                    }

                    if(data[0].equalsIgnoreCase("CHECK")) {
                        Check check = new Check();

                        check.setAmount(Double.valueOf(data[1]));
                        check.setPaymentDate(GlobalConst.simpleDateFormat.parse(data[2].trim()));

                        check.setCheckNumber(data[3].trim());
                        check.setBankName(data[4].trim());
                        check.setBranchName(data[5].trim());

                        Character character = data[6].charAt(0);
                        switch (character) {
                            case 'Y':
                                check.setCleared(Boolean.TRUE);
                                break;
                            case 'N':
                                check.setCleared(Boolean.FALSE);
                                break;
                            default:
                                check.setCleared(Boolean.FALSE);
                        }

                        dao.save(check);

                    }

                    if (data[0].equalsIgnoreCase("CREDIT_CARD")) {
                        CreditCard creditCard = new CreditCard();
                        creditCard.setAmount(Double.valueOf(data[1]));
                        creditCard.setPaymentDate(GlobalConst.simpleDateFormat.parse(data[2].trim()));
                        creditCard.setCreditCardNumber(data[3].trim());
                        creditCard.setExpireyDate(GlobalConst.simpleDateFormat.parse(data[4].trim()));
                        String s = data[5].trim().toString();

                        if (s.equals("AMEX")) {
                            creditCard.setCreditCardType(CCType.AMEX);

                        } else if (s.equals("VISA")) {
                            creditCard.setCreditCardType(CCType.VISA);

                        } else if (s.equals("MASTER")) {
                            creditCard.setCreditCardType(CCType.MASTER);

                        } else {
                            creditCard.setCreditCardType(CCType.AMEX);

                        }

                        dao.save(creditCard);
                    }



                    line = br.readLine();

                }

                br.close();

                }catch(Exception e ){
                    e.printStackTrace();
                }

    }
}
